package com.easoft.code.pos.ui.controllers;

import com.easoft.code.pos.data.dao.GenericDao;
import com.easoft.code.pos.data.exception.CoreDataLayerException;
import com.easoft.code.pos.data.util.QueryParameter;
import com.easoft.code.pos.ui.controllers.exception.ControllerException;
import com.easoft.code.pos.ui.models.PojoListModel;
import com.easoft.code.pos.util.ComboItem;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author Eliu
 */
public class InventariosMovController {

    private GenericDao dao = GenericDao.getMe();

    /**
     *
     * @param entity
     * @return
     * @throws ControllerException
     */
    public PojoListModel getListModelByEntity(Class entity) throws ControllerException {
        try {
            return new PojoListModel((List) dao.doNamedQuery(entity.getSimpleName().concat(".findAll"),
                    new QueryParameter[]{}, entity, false));
        } catch (CoreDataLayerException ex) {
            throw new ControllerException("Error obteniendo PojoModel", ex);
        }
    }

    /**
     *
     * @return
     */
    public PojoListModel getEstadosListModel() {
        return new PojoListModel(Arrays.asList(new ComboItem("1", "APLICADO"),
                new ComboItem("0", "ANULADO"), new ComboItem("2", "PENDIENTE")));

    }

    /**
     *
     * @param serie
     * @return
     */
    public Integer getNextDocNumber(String serie) {
        try {
            Number n = (Number) dao.doJQL("select max(t.numero) as nxtNumero from DocumentoOper t where t.serie=:pserie",
                    new QueryParameter[]{new QueryParameter("pserie", serie)}, Number.class, true);

            return n == null ? 1 : (n.intValue() + 1);
        } catch (CoreDataLayerException ex) {
            ex.printStackTrace(System.err);
            return 0;
        }
    }
}
